#pragma once
#include <vector>

namespace zh
{
	template<class T>
	struct less
	{
		bool operator()(const T& t1,const T& t2)
		{return t1 < t2;}
	};

	template<class T>
	struct greater
	{
		bool operator()(const T& t1,const T& t2)
		{return t1 > t2;}
	};

	template<class T, class Container = std::vector<T>, class Compare = less<T>>
	class priority_queue
	{
	private:
		void adjust_up(size_t pos)
		{
			Compare com;
			if (pos == 0)return;
			while (pos > 0)
			{
				int prev = (pos - 1) / 2;
				if (com(_con[prev], _con[pos]))
				{
				::swap(_con[pos], _con[prev]);
					pos = prev;
				}
				else
					return;
			}
		}
		void adjust_down(size_t father = 0)
		{
			Compare com;
			if (_con.size() < 2)return;
			size_t last = (_con.size() - 2) / 2;
			while (1)
			{
				if (father > last)
					return;
				size_t child = father * 2 + 1;
				if (child < _con.size() - 1 &&
					com(_con[child], _con[child + 1]))
					++child;
				::swap(_con[child], _con[father]);
				father = child;
			}
		}
	public:
		void push(const T& t)
		{
			_con.push_back(t);
			adjust_up(_con.size() - 1);
		}
		void pop()
		{
			if (_con.size() > 1)
				::swap(_con[0], _con[size()-1]);
			_con.pop_back();
			adjust_down();
		}
		T& top()
		{
			return _con[0];
		}
		size_t size()
		{
			return _con.size();
		}
		bool empty()
		{
			return _con.empty();
		}
		T& operator[](size_t n)
		{
			return _con[n];
		}
	private:
		Container _con;
	};
}